package top.yuyufeng.web.mobileServlet;

import top.yuyufeng.web.utils.MyImageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Map;

/**
 * @author yuyufeng
 * @date 2018/1/22
 */
@WebServlet(name = "ValidImageServletMB", urlPatterns = "/validImageMB")
public class ValidImageServletMB extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Map<String,String> map = (Map<String, String>) session.getAttribute("validMap");


        if(map == null){
            Writer writer = response.getWriter();
            writer.write("非法获取");
            writer.flush();
            return;
        }

        int x = Integer.valueOf(map.get("location").split(",")[0]);
        int y = Integer.valueOf(map.get("location").split(",")[1]);

        try {
            java.awt.Rectangle rectangle = new java.awt.Rectangle(0, 0, 312, 155);
            java.awt.Rectangle drugRectangle = new java.awt.Rectangle(x, y, 55, 55);
            OutputStream os = response.getOutputStream();
            MyImageUtil.cutAndDrugImage(new File("c://test//upload_mb/"+map.get("image")),os,rectangle,drugRectangle);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
